package edu.whut.mall.admin.service.impl;

import com.github.pagehelper.PageHelper;
import edu.whut.mall.admin.domain.GroupBuyCategoryParams;
import edu.whut.mall.admin.domain.GroupBuyRuleParams;
import edu.whut.mall.admin.enums.SysStatusEnum;
import edu.whut.mall.admin.mapper.GroupBuyRuleCustomMapper;
import edu.whut.mall.admin.service.IGroupBuyService;
import edu.whut.mall.admin.shiro.util.ShiroUtil;
import edu.whut.mall.admin.vo.GroupBuyCategoryVo;
import edu.whut.mall.admin.vo.GroupBuyRuleVo;
import edu.whut.mall.common.api.CommonPage;
import edu.whut.mall.mapper.GroupBuyCategoryMapper;
import edu.whut.mall.mapper.GroupBuyRuleMapper;
import edu.whut.mall.model.GroupBuyCategory;
import edu.whut.mall.model.GroupBuyCategoryExample;
import edu.whut.mall.model.GroupBuyRule;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @program: d9lab-mall
 * @description: 团购功能实现类
 * @author: Wayne
 * @create: 2020-05-15 15:22
 **/
@Service
@RequiredArgsConstructor
@Slf4j
public class GroupBuyServiceImpl implements IGroupBuyService {

    private final GroupBuyCategoryMapper groupBuyCategoryMapper;

    private final GroupBuyRuleMapper groupBuyRuleMapper;

    private final GroupBuyRuleCustomMapper groupBuyRuleCustomMapper;

    @Override
    public Integer insertOneGroupBuyCategory(GroupBuyCategoryParams groupBuyCategoryParams) {
        GroupBuyCategory groupBuyCategory = new GroupBuyCategory();
        BeanUtils.copyProperties(groupBuyCategoryParams,groupBuyCategory);
        groupBuyCategory.setStatus(SysStatusEnum.NOT_AVAILABLE.getCode());
        return groupBuyCategoryMapper.insertSelective(groupBuyCategory);
    }

    @Override
    public Integer updateOneGroupBuyCategory(GroupBuyCategoryParams groupBuyCategoryParams) {
        GroupBuyCategory groupBuyCategory = new GroupBuyCategory();
        BeanUtils.copyProperties(groupBuyCategoryParams,groupBuyCategory);
        groupBuyCategory.setUpdateTime(new Date());
        return groupBuyCategoryMapper.updateByPrimaryKeySelective(groupBuyCategory);
    }

    @Override
    public Integer updateOneGroupBuyCategoryStatus(Long categoryId, Integer status) {
        GroupBuyCategory groupBuyCategory = new GroupBuyCategory();
        groupBuyCategory.setId(categoryId);
        groupBuyCategory.setStatus(status);
        return groupBuyCategoryMapper.updateByPrimaryKeySelective(groupBuyCategory);
    }

    @Override
    public Integer deleteOneGroupBuyCategory(Long categoryId) {
        GroupBuyCategory groupBuyCategory = new GroupBuyCategory();
        groupBuyCategory.setId(categoryId);
        groupBuyCategory.setIsDeleted(1);
        return groupBuyCategoryMapper.updateByPrimaryKeySelective(groupBuyCategory);
    }

    @Override
    public Integer insertOneGroupBuyRule(GroupBuyRuleParams groupBuyRuleParams) {
        GroupBuyRule groupBuyRule = new GroupBuyRule();
        BeanUtils.copyProperties(groupBuyRuleParams,groupBuyRule);
        groupBuyRule.setSuccessGroupNumber(0);
        groupBuyRule.setStatus(SysStatusEnum.NOT_AVAILABLE.getCode());
        return groupBuyRuleMapper.insertSelective(groupBuyRule);
    }

    @Override
    public Integer updateOneGroupBuyRule(Long ruleId, GroupBuyRuleParams groupBuyRuleParams) {
        GroupBuyRule groupBuyRule = new GroupBuyRule();
        BeanUtils.copyProperties(groupBuyRuleParams,groupBuyRule);
        groupBuyRule.setId(ruleId);
        return groupBuyRuleMapper.updateByPrimaryKeySelective(groupBuyRule);
    }

    @Override
    public Integer updateOneGroupBuyRuleStatus(Long ruleId, Integer status) {
        GroupBuyRule groupBuyRule = new GroupBuyRule();
        groupBuyRule.setId(ruleId);
        groupBuyRule.setStatus(status);
        return groupBuyRuleMapper.updateByPrimaryKeySelective(groupBuyRule);
    }

    @Override
    public Integer deleteOneGroupBuyRule(Long ruleId) {
        GroupBuyRule groupBuyRule = new GroupBuyRule();
        groupBuyRule.setId(ruleId);
        groupBuyRule.setIsDeleted(1);
        return groupBuyRuleMapper.updateByPrimaryKeySelective(groupBuyRule);
    }

    @Override
    public CommonPage<GroupBuyCategoryVo> listGroupBuyCategoryVos(Integer pageNum, Integer size) {
        PageHelper.startPage(pageNum, size);
        GroupBuyCategoryExample groupBuyCategoryExample = new GroupBuyCategoryExample();
        groupBuyCategoryExample.createCriteria().andIsDeletedEqualTo(0);
        List<GroupBuyCategory> groupBuyCategoryList = groupBuyCategoryMapper.selectByExample(groupBuyCategoryExample);
        List<GroupBuyCategoryVo> groupBuyCategoryVoList = groupBuyCategoryList.stream().map(this::transCategory2Vo).collect(Collectors.toList());
        return CommonPage.restPage(groupBuyCategoryVoList);
    }

    @Override
    public CommonPage<GroupBuyRuleVo> listGroupBuyRuleByGroupBuyCategoryId(Long categoryId, Long merchantId, Integer pageNum, Integer size) {
        PageHelper.startPage(pageNum, size);
        Long currentMerchantId = merchantId == null ? ShiroUtil.getMerchantId() : merchantId;
        List<GroupBuyRuleVo> groupBuyRuleVoList = groupBuyRuleCustomMapper.listGroupBuyRuleVoByGroupBuyCategoryId(categoryId, currentMerchantId);
        return CommonPage.restPage(groupBuyRuleVoList);
    }

    /**
     * 将团购分类转换成展示层
     * @param groupBuyCategory
     * @return
     */
    public GroupBuyCategoryVo transCategory2Vo(GroupBuyCategory groupBuyCategory){
        GroupBuyCategoryVo groupBuyCategoryVo = new GroupBuyCategoryVo();
        BeanUtils.copyProperties(groupBuyCategory, groupBuyCategoryVo);
        return groupBuyCategoryVo;
    }

    /**
     * 将团购规则转换成展示层
     * @param groupBuyRule
     * @return
     */
    public GroupBuyRuleVo transRule2Vo(GroupBuyRule groupBuyRule){
        GroupBuyRuleVo groupBuyRuleVo = new GroupBuyRuleVo();
        BeanUtils.copyProperties(groupBuyRule, groupBuyRuleVo);
        return groupBuyRuleVo;
    }
}
